package Utils.excel;

import Utils.FsUtils;
import Utils.data.StringUtils;
import Utils.io.IOUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.jupiter.api.Test;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;

public class Txt2XlsxForAnki {
    @Test
    public void test01() throws Exception {
        String path = "D:\\个人文件\\学习\\业余无线电\\业余无线电台执照考试题库\\业余电台操作证书考试题库TXT题库包(v20211022)\\总题库(v20211022)（A为正确答案）.txt";
        File file = new File(path);

        FileInputStream is = new FileInputStream(file);
        InputStreamReader isr = new InputStreamReader(is, "GB2312");
        BufferedReader br = new BufferedReader(isr);
        String line;

        ArrayList<Quiz> quizzes = new ArrayList<>();
        ArrayList<Quiz> quizzes1 = new ArrayList<>();
        String question = "";
        ArrayList<String> options = new ArrayList<String>();
        Integer answer = 1;
        String comment = "无";
        try {
            while ((line = br.readLine()) != null) {
                if (line.startsWith("[I]")) {
                    question = "[" + line.substring(3) + "]";
                }
                if (line.startsWith("[Q]")) {
                    question += line.substring(3);
                }
                if (line.startsWith("[A]")) {
                    options.add(line.substring(3));
                }
                if (line.startsWith("[B]")) {
                    options.add(line.substring(3));
                }
                if (line.startsWith("[C]")) {
                    options.add(line.substring(3));
                }
                if (line.startsWith("[D]")) {
                    options.add(line.substring(3));
                }
                if (line.startsWith("[P]")) {
                    String answer0 = options.get(0);
                    Collections.shuffle(options);
                    StringBuilder optionsStr = new StringBuilder();
                    StringBuilder optionsStr1 = new StringBuilder();
                    for (int i = 0; i < options.size(); i++) {
                        optionsStr.append(options.get(i)).append("\n");
                        optionsStr1.append((char) ((int) 'A' + i)).append(". ").append(options.get(i)).append("\n");
                        if (answer0.equals(options.get(i))) {
                            answer = i + 1;
                        }
                    }
                    quizzes.add(new Quiz(question, optionsStr.toString(), answer, comment));
                    quizzes1.add(new Quiz(question, optionsStr1.toString(), answer, comment));
                    options.clear();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            is.close();
            isr.close();
            br.close();
        }

        PoiUtils.entryList2Xlsx(quizzes, "D:\\个人文件\\学习\\业余无线电\\业余无线电台执照考试题库\\业余电台操作证书考试题库TXT题库包(v20211022)\\" + FsUtils.getNameExcludingSuffix(path) + ".xlsx", 10, true);
        IOUtils.String2File(quizzes1.toString(), "D:\\个人文件\\学习\\业余无线电\\业余无线电台执照考试题库\\业余电台操作证书考试题库TXT题库包(v20211022)\\new_" + FsUtils.getNameExcludingSuffix(path) + ".txt");
    }

    @Test
    public void test02() {
        Quiz quiz = new Quiz("q", "o", 1, "c");
        System.out.println(quiz);
    }

    @Test
    public void test03() {
        int a = 'A';
        System.out.println(a);
        char a1 = (char) a;
        System.out.println(a1);
    }

    @Test
    public void test04() throws Exception {
        String path = "D:\\个人文件\\学习\\CISA\\05.html";
        File file = new File(path);
        String html = IOUtils.file2String(path);
        ArrayList<Quiz> quizzes = new ArrayList<>();

        Document doc = Jsoup.parse(html);
        Elements hQuizzes = doc.getElementsByClass("start-left-item");
        for (int i = 0; i < hQuizzes.size(); i++) {
            Quiz quiz = new Quiz();
            Element hQuiz = hQuizzes.get(i);
            Elements title = hQuiz.getElementsByClass("question-title");
            quiz.setQuestion(title.text().replace("收藏", "").replace("纠错", "").replace("取消", ""));

            Elements options = hQuiz.getElementsByClass("radio");
            StringBuilder opts = new StringBuilder();
            for (Element option : options) {
                opts.append(option.getElementsByTag("p").get(0).text()).append("\n");
            }
            quiz.setOptions(opts.toString().trim());

            Element answer = hQuiz.getElementsByAttributeValue("style", "color: rgb(0, 175, 199);").get(0);
            char a = answer.text().charAt(0);
            int ans = (int) a - 'A' + 1;
            quiz.setAnswer(ans);

            Element analysis = hQuiz.getElementsByClass("analys_bottom").get(0);
            Element p = analysis.getElementsByTag("p").get(1);
            quiz.setComment(p.text());

            quizzes.add(quiz);
        }


        PoiUtils.entryList2Xlsx(quizzes, "D:\\个人文件\\学习\\CISA\\" + FsUtils.getNameExcludingSuffix(path) + ".xlsx", 10, true);
    }

    @Test
    public void test05() throws Exception {
        String path = "D:\\个人文件\\学习\\CISA\\2022年ISACA CISA备考模拟题集.txt";
        File file = new File(path);

        FileInputStream is = new FileInputStream(file);
        InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8);
        BufferedReader br = new BufferedReader(isr);
        String line;

        ArrayList<Quiz> quizzes = new ArrayList<>();
        String question = "";
        ArrayList<String> options = new ArrayList<String>();
        Integer answer = 1;
        String comment = "无";
        StringBuilder optionsStr = new StringBuilder();
        try {
            while ((line = br.readLine()) != null) {
                /*if (line.matches("^\\d+")) {
                    question = line;
                }*/
                if (StringUtils.isMatch(line, "^\\d+")) {
                    question = line;
                }
                if (line.startsWith("a ")) {
                    options.add(line.substring(2).trim());
                }
                if (line.startsWith("b ")) {
                    options.add(line.substring(2).trim());
                }
                if (line.startsWith("c ")) {
                    options.add(line.substring(2).trim());
                }
                if (line.startsWith("d ")) {
                    options.add(line.substring(2).trim());
                }
                if (line.startsWith("答案")) {
                    line = line.trim();
                    char answerChar = line.substring(line.length() - 1).toLowerCase().charAt(0);
                    answer = (int) answerChar - 'a' + 1;
                    optionsStr.setLength(0);
                    for (int i = 0; i < options.size(); i++) {
                        optionsStr.append(options.get(i)).append("\n");
                    }
                }
                if (line.startsWith("说明：")) {
                    comment = line;
                    quizzes.add(new Quiz(question, optionsStr.toString().trim(), answer, comment));
                    options.clear();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            is.close();
            isr.close();
            br.close();
        }

        PoiUtils.entryList2Xlsx(quizzes, "D:\\个人文件\\学习\\CISA\\" + FsUtils.getNameExcludingSuffix(path) + ".xlsx", 10, true);
    }

    @Test
    public void test06() throws Exception {
        String path = "D:\\个人文件\\学习\\CISA\\202201全真考前冲刺试卷.txt";
        File file = new File(path);

        FileInputStream is = new FileInputStream(file);
        InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8);
        BufferedReader br = new BufferedReader(isr);
        String line;

        ArrayList<Quiz> quizzes = new ArrayList<>();
        String question = "";
        ArrayList<String> options = new ArrayList<String>();
        Integer answer = 1;
        String comment = "无";
        StringBuilder optionsStr = new StringBuilder();
        try {
            while ((line = br.readLine()) != null) {
                if (StringUtils.isMatch(line, "^\\d+")) {
                    question = line;
                }
                if (line.startsWith("A")) {
                    options.add(line.substring(2).trim());
                }
                if (line.startsWith("B")) {
                    options.add(line.substring(2).trim());
                }
                if (line.startsWith("C")) {
                    options.add(line.substring(2).trim());
                }
                if (line.startsWith("D")) {
                    options.add(line.substring(2).trim());

                    optionsStr.setLength(0);
                    for (int i = 0; i < options.size(); i++) {
                        optionsStr.append(options.get(i)).append("\n");
                    }
                    quizzes.add(new Quiz(question, optionsStr.toString().trim(), answer, comment));
                    options.clear();
                }
                if (line.startsWith("答案")) {
                    line = line.trim();
                    char answerChar = line.substring(line.length() - 1).toLowerCase().charAt(0);
                    answer = (int) answerChar - 'a' + 1;

                }
                if (line.startsWith("说明：")) {
                    comment = line;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            is.close();
            isr.close();
            br.close();
        }

        PoiUtils.entryList2Xlsx(quizzes, "D:\\个人文件\\学习\\CISA\\" + FsUtils.getNameExcludingSuffix(path) + ".xlsx", 10, true);
    }

    @Test
    public void test07() throws Exception {
        String path = "D:\\个人文件\\学习\\CISA\\202212第二部分.txt";
        File file = new File(path);

        FileInputStream is = new FileInputStream(file);
        InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8);
        BufferedReader br = new BufferedReader(isr);
        String line;

        ArrayList<Quiz> quizzes = new ArrayList<>();
        String question = "";
        ArrayList<String> options = new ArrayList<>();
        Integer answer = 1;
        String comment = "无";
        StringBuilder optionsStr = new StringBuilder();
        try {
            while ((line = br.readLine()) != null) {
                if (StringUtils.isMatch(line, "^\\d+")) {
                    if (!"".equals(question)) {
                        optionsStr.setLength(0);
                        for (int i = 0; i < options.size(); i++) {
                            optionsStr.append(options.get(i)).append("\n");
                        }
                        quizzes.add(new Quiz(question, optionsStr.toString().trim(), answer, comment));
                        options.clear();
                        comment = "无";
                    }

                    question = line;
                } else if (
                        StringUtils.isMatch(line, "^[ABCDEabcde]")) {
                    options.add(line.replaceAll("^[ABCDEabcde]\\.", "").replaceAll("^[ABCDEabcde]", "").trim());
                } else if (line.startsWith("答案")) {
                    line = line.trim();
                    char answerChar = line.substring(line.length() - 1).toLowerCase().charAt(0);
                    answer = (int) answerChar - 'a' + 1;

                } else if (!line.trim().isEmpty()) {
                    comment = line;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            is.close();
            isr.close();
            br.close();
        }

        PoiUtils.entryList2Xlsx(quizzes, "D:\\个人文件\\学习\\CISA\\" + FsUtils.getNameExcludingSuffix(path) + ".xlsx", 10, true);
    }
}
